package org.darkforest.ucenter.service.impl;

import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.PointsLogMapper;
import org.darkforest.dao.mapper.PointsOrderMapper;
import org.darkforest.dao.mapper.SettingMapper;
import org.darkforest.enums.MemberType;
import org.darkforest.enums.PointTypeEnum;
import org.darkforest.enums.PointsOrderStatusEnum;
import org.darkforest.model.*;
import org.darkforest.ucenter.service.MemberAccountService;
import org.darkforest.ucenter.service.PointService;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 项目名称         :bwmall-ucenter
 * 文件名           :PointServiceImpl.java
 * 开发公司及作者    :陈超
 * 版本号及开发日期  :Rev. 1.00 (2015年2月15日)
 */
@Service
public class PointServiceImpl implements PointService {
	
	@Autowired
	private PointsLogMapper pointsLogMapper;
	@Autowired
	private PointsOrderMapper pointsOrderMapper;
	@Autowired
	private SettingMapper settingMapper;				//获取配置
	@Autowired
	private MemberAccountService memberAccountService;				//更新用户账户状态

	@Override
	public void findPointsLogByMember(PointsLogSearchBean page) {
//		page.setResultList(pointsLogMapper.selectByMember(page));
		List<PointsLogVo> res = pointsLogMapper.selectByMemberGroupByStage(page);
		if(res!=null && res.size()>0){
			for (PointsLogVo logVo : res) {
				if (logVo.getPlStage().equals(PointTypeEnum.RECOMMEND_REGIST.getValue())) {
					//获取desc中拼接的用户列表，并还原成为列表模式
					String[] users = logVo.getPlDesc().split(",");
					List<MemberBase> list = new ArrayList<MemberBase>();
					for (String user : users) {
						String[] detail = user.split(";");
						list.add(new MemberBase(null, detail[0], detail[1]));
					}
					logVo.setRecommendMemberList(list);
				}
			}
		}
		page.setResultList(res);
	}


	@Override
	public void findPointsOrderByMember(PointsOrderSearchBean page) {
		page.setResultList(pointsOrderMapper.selectByMember(page));
	}

	@Override
	public PointsOrderVo findPointsOrderById(Integer pointOrderid, Integer memberId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pointOrderid", pointOrderid);
		param.put("memberId", memberId);
		return pointsOrderMapper.selectDetailByMember(param);
	}

	@Override
	public void takePointsOrderById(Integer pointOrderid, Integer memberId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("pointOrderid", pointOrderid);
		param.put("memberId", memberId);
		PointsOrderVo vo = pointsOrderMapper.selectDetailByMember(param);
		if(vo != null){
			PointsOrder order = new PointsOrder();
			order.setPointOrderid(pointOrderid);
			order.setPointOrderstate(PointsOrderStatusEnum.TAKE.getValue());
			pointsOrderMapper.updateByPrimaryKeySelective(order);
		}
		
	}

	/**
	 * 注册送积分 TODO 增加事务
	 * @param recommendId	无推荐人时传null
	 * @param recommendName  无推荐人时传null
	 * @param memberId
	 * @param memberName
	 * @param memberAvatar
	 * @return
	 */
	@Override
	public void addPointForRegist(Integer recommendId,String recommendName,Integer parentType,Integer memberId,String memberName,String memberAvatar) {
		//推荐人不为空且推荐人不为代理商时，推荐人增加相应的推荐积分
		if(recommendId!=null && recommendId!=0 && parentType!=null && parentType.equals(MemberType.NORMAL.getType())){
			String points = settingMapper.selectByPrimaryKey("recommend_regist_points").getValue();
			this.addMemberPoint(recommendId,recommendName,Integer.parseInt(points),PointTypeEnum.RECOMMEND_REGIST, memberName+";"+memberAvatar);		//描述中存放被推荐人的名字与头像
		}
		String registPoint = settingMapper.selectByPrimaryKey("regist_points").getValue();
		this.addMemberPoint(memberId,memberName,Integer.parseInt(registPoint),PointTypeEnum.REGIST,"");
	}

	/**
	 * 增加point_log数据，修改member表积分字段数值 TODO 增加事务
	 * @param memberId
	 * @param memberName
	 * @param points
	 * @param desc
	 * @return
	 */
	@Override
	public int addMemberPoint(Integer memberId, String memberName, Integer points,PointTypeEnum pointType, String desc) {
		int res = 0;
		PointsLog log = new PointsLog();
		log.setPlMemberid(memberId);
		log.setPlMembername(memberName);
		log.setPlPoints(new BigDecimal(points));
		log.setPlAddtime(DateUtil.unixTimeStampInt().intValue());
		log.setPlStage(pointType.getValue());
		log.setPlState(1);
		log.setPlDesc(desc);
		int logRes = pointsLogMapper.insert(log);
		if(logRes>0){
			res = memberAccountService.updateMemberPoint(memberId,points,null);
		}
		return res;
	}

	/**
	 * 仅增加日志，pl_state记为0，操作账户表冻结字段 TODO 添加事务
	 * @param memberId
	 * @param memberName
	 * @param points
	 * @param pointType
	 * @param desc
	 * @return
	 */
	@Override
	public int addMemberFrozenPoint(Integer memberId, String memberName, Integer points, PointTypeEnum pointType, String desc) {
		int res = 0;
		PointsLog log = new PointsLog();
		log.setPlMemberid(memberId);
		log.setPlMembername(memberName);
		log.setPlPoints(new BigDecimal(points));
		log.setPlAddtime(DateUtil.unixTimeStampInt().intValue());
		log.setPlStage(pointType.getValue());
		log.setPlState(0);
		log.setPlDesc(desc);
		int logRes = pointsLogMapper.insert(log);
		if(logRes>0){
			res = memberAccountService.updateMemberPoint(memberId,null,points);
		}
		return res;
	}
}
